{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "11a70c15",
   "metadata": {},
   "source": [
    "------------\n",
    "* 2022-10-18 Week09\n",
    "* Python课堂笔记\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48a9f5c5",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "> 1.while循环  \n",
    "> 2.购物车练习  \n",
    "> 3.元组和集合  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fe4d7332",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯 0\n",
      "你是风儿我是沙，缠缠绵绵到天涯 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯 5\n",
      "你是风儿我是沙，缠缠绵绵到天涯 6\n",
      "你是风儿我是沙，缠缠绵绵到天涯 7\n",
      "你是风儿我是沙，缠缠绵绵到天涯 8\n",
      "你是风儿我是沙，缠缠绵绵到天涯 9\n"
     ]
    }
   ],
   "source": [
    "#while Ture 可以实现for+range的方法\n",
    "#while Ture一般用于不确定的循环次数或者无限次循环操作\n",
    "count = 0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯\",count)  \n",
    "    count +=1\n",
    "    if count ==10:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "68094db1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:90\n"
     ]
    }
   ],
   "source": [
    "#猜数字小游戏： 0<n<50,5次机会猜数字，每次猜完出题人提示你猜的有多大/有点小\n",
    "\n",
    "hide_card = int(input(\"hide_card:\"))#出题人\n",
    "count = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7eecbf4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card········\n",
      "游戏开始了\n",
      "guess_digit:90\n",
      "往小了想想\n",
      "guess_digit:70\n",
      "往大了想想\n",
      "guess_digit:76\n",
      "往大了想想\n",
      "guess_digit:80\n",
      "yes,you got it\n"
     ]
    }
   ],
   "source": [
    "#猜数字小游戏： 0<n<50,5次机会猜数字，每次猜完出题人提示你猜的有多大/有点小\n",
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card\"))#出题人\n",
    "count = 0\n",
    "\n",
    "#peint(hide_card)\n",
    "\n",
    "if hide_card<100 and hide_card> 0:\n",
    "    print(\"游戏开始了\")\n",
    "    while True:\n",
    "        if count<5:\n",
    "            guess_digit = int(input(\"guess_digit:\"))  #玩游戏的人\n",
    "            if guess_digit == hide_card:\n",
    "                print(\"yes,you got it\")\n",
    "                break\n",
    "            elif guess_digit > hide_card:\n",
    "                print(\"往小了想想\")\n",
    "            else:\n",
    "                print(\"往大了想想\")\n",
    "                count = count+1\n",
    "        else:\n",
    "                print(\"猜这么多次都不对，你个笨蛋\")\n",
    "                print(\"正确答案是%s\" %hide_card)\n",
    "                break\n",
    "\n",
    "else:\n",
    "    print(\"请输入数字在0-99之间，不要偷懒\")\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c34bec60",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(Good_list[i][0],Good_list[i][1])    \n",
    "print(Good_list[1][0],Good_list[1][1])   #0代表取出列表第一个值，再个值取出价格\n",
    "print(Good_list[2][0],Good_list[2][1])              \n",
    "print(Good_list[3][0],Good_list[3][1])\n",
    "\n",
    "#寻找共同点，用for+range循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2e2a906",
   "metadata": {},
   "source": [
    "# 购物车"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a62e1005",
   "metadata": {},
   "source": [
    "> 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户    \n",
    "> 2.让用户输入商品编号进行商品的购买  \n",
    "> 3.用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户  \n",
    "> 4.用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "052d6c1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额:9000\n",
      "------苹果商城列表-------\n",
      "0 Iphone14 5000元\n",
      "1 Iphone pro14 8999元\n",
      "2 Macbook 10000元\n",
      "请输入商品编号:1\n",
      "购买成功，请等待商品出货，您的余额还有：1元\n",
      "------苹果商城列表-------\n",
      "0 Iphone14 5000元\n",
      "1 Iphone pro14 8999元\n",
      "2 Macbook 10000元\n",
      "请输入商品编号:2\n",
      "您的余额不足10000钱购买,请更换交付方式或选择其他商品\n",
      "------苹果商城列表-------\n",
      "0 Iphone14 5000元\n",
      "1 Iphone pro14 8999元\n",
      "2 Macbook 10000元\n",
      "请输入商品编号:2\n",
      "您的余额不足10000钱购买,请更换交付方式或选择其他商品\n",
      "------苹果商城列表-------\n",
      "0 Iphone14 5000元\n",
      "1 Iphone pro14 8999元\n",
      "2 Macbook 10000元\n",
      "请输入商品编号:q\n",
      "感谢您的购买，欢迎下次光顾\n",
      "您的购物车: [['Iphone pro14', 8999]]\n",
      "您的余额还有: 1\n"
     ]
    }
   ],
   "source": [
    "# 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户  \n",
    "\n",
    "Good_list = [[\"Iphone14\",5000],\n",
    "      [\"Iphone pro14\",8999],\n",
    "      [\"Macbook\", 10000]]\n",
    "#数据存放，列表的增加（append）\n",
    "shopping_list = []  \n",
    "Alipay= int(input(\"请输入余额:\"))\n",
    "            \n",
    "while True:\n",
    "    print(\"------苹果商城列表-------\")\n",
    "    for i in range(3):\n",
    "        print(i,Good_list[i][0],str(Good_list[i][1])+\"元\")    #0代表取出列表第一个值，再个值取出价格\n",
    "\n",
    "#2.让用户输入商品编号进行商品的购买。            \n",
    "    user_choice =input(\"请输入商品编号:\")           \n",
    "\n",
    "#3.用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户 \n",
    "#3.1 用户输入商品序号（数值）\n",
    "    if user_choice.isdigit() and int(user_choice)<len(Good_list) and int (user_choice)>=0:\n",
    "        user_choice=int(user_choice)\n",
    "        if Alipay > Good_list[user_choice][1]:\n",
    "        #余额足够\n",
    "            Alipay = Alipay - Good_list[user_choice][1] #更新用户余额\n",
    "            shopping_list.append(Good_list[user_choice])\n",
    "            print(\"购买成功，请等待商品出货，您的余额还有：%s元\"%(Alipay)) \n",
    "        else:\n",
    "        #余额不够\n",
    "            print(\"您的余额不足\"+str(Good_list[user_choice][1])+\"钱购买,请更换交付方式或选择其他商品\")\n",
    "#3.2 用户输入其他\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"感谢您的购买，欢迎下次光顾\")\n",
    "        print(\"您的购物车:\",shopping_list)\n",
    "        print(\"您的余额还有:\",Alipay)\n",
    "        break\n",
    "    else:\n",
    "        print(\"您输入的商品不存在或者输入的内容有误！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3940f7c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "0c158630",
   "metadata": {},
   "source": [
    "## 课后学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c75ebb2c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#集合\n",
    "vowel = {'a','e','i','o','u','u'}\n",
    "vowel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "86053a52",
   "metadata": {},
   "outputs": [],
   "source": [
    "#set函数生成集合\n",
    "vowel =set('aeiouu') #集合不允许有重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d82423aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e0ea9683",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'l', 'o', 'u', 'v'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将字符串转为集合 set\n",
    "vowel = {'a','e','i','o','u','u'}\n",
    "word = 'love'\n",
    "\n",
    "#sorted，list将集合转为有序列表 \n",
    "u_list=sorted(list(u))\n",
    "u_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "27d7e005",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'i', 'l', 'o', 'u', 'v']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#union将集合进行合并 并集\n",
    "u=vowel.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "42fa63da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#difference分清哪些不是共有元素  差集\n",
    "d= vowel.difference(set(word))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "824b4155",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#intersection报告共有对象  交集\n",
    "p=vowel.intersection(set(word))\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cbace3e8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowelsexpend\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "vowel=set('aeiouu')\n",
    "word=input(\"provide a word to search for vowels\")\n",
    "found=vowel.intersection(word)\n",
    "for vowel in found:\n",
    "    print(vowel)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25885f51",
   "metadata": {},
   "outputs": [],
   "source": [
    "#元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bc905167",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'p', 'i', 'u', 'y'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "list=['o','p','i','u','y'] #列表是中括号\n",
    "print(list,type(list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "41d3f815",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('o', 'p', 'i', 'u', 'y') <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "vowel =('o','p','i','u','y') #元组是小括号\n",
    "print(vowel,type(vowel))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "60f4a4ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'p', 't', 'u', 'y']\n"
     ]
    }
   ],
   "source": [
    "#元组是不可变的，列表是可变的\n",
    "list=['o','p','i','u','y']\n",
    "list[2]=\"t\" #改变列表里的元素\n",
    "print(list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ca8458ae",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[0;32mIn [10]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m vowel \u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mo\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mp\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mi\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mu\u001b[39m\u001b[38;5;124m'\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124my\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m vowel[\u001b[38;5;241m2\u001b[39m]\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mI\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(vowel)\n",
      "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "#元组是不可变的，列表是可变的\n",
    "vowel =('o','p','i','u','y')\n",
    "vowel[2]=\"I\"\n",
    "print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c178acf4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "python <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "#元组在小括号中至少包括一个逗号\n",
    "t = ('python') #注意此时是字符串\n",
    "print(t,type(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ca79bc97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('python',) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "t = ('python',) #注意此时加了逗号\n",
    "print(t,type(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "593dc8cc",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
